﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter14.Code04.Example03
{
    using Microsoft.CSharp.RuntimeBinder;

    public class Demo
    {
        private static int callCout = 0;

        public static void Begin()
        {
            try
            {
                dynamic firstResult = GetValue();
                dynamic secondResult = GetValue();
                Console.WriteLine("firstResult is: {0}", firstResult.ToString());
                Console.WriteLine("firstResult is: {0}", secondResult.ToString());
                Console.WriteLine("firstResult call: {0}", firstResult.Add(2, 3));
                Console.WriteLine("firstResult call: {0}", secondResult.Add(2, 4));
            }
            catch (RuntimeBinderException ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }

        private static dynamic GetValue()
        {
            if (callCout++ == 0)
            {
                return new MyClass1();
            }
            return new MyClass2();
        }
    }
}
